package com.imooc.controller;

import com.imooc.VO.ResultVO;
import com.imooc.converter.OrderForm2OrderDTOConverter;
import com.imooc.dto.OrderDTO;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import com.imooc.form.OrderForm;
import com.imooc.service.BuyerOrderService;
import com.imooc.service.OrderMasterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 买家订单
 * @author liuhuan
 * @date 2018/7/7 14:16
 * @description
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {



    @Autowired
    private OrderMasterService orderMasterService;

    @Autowired
    private BuyerOrderService buyerOrderService;
    //创建订单
    @PostMapping("create")
    public ResultVO<Map<String,Object>> create(@Valid OrderForm orderForm,
                                               BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("【创建订单】参数不正确,orderForm={}" ,orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO= OrderForm2OrderDTOConverter.convert(orderForm);
        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.error("【创建订单】购物车为空,orderDetailList={}" ,orderDTO.getOrderDetailList());
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDTO result=orderMasterService.create(orderDTO);
        Map<String,Object> map=new HashMap<>();
        map.put("orderId",result.getOrderId());

        return ResultVO.success(map);

    }

    //订单列表

    /**
     * 查询订单列表
     * @param openId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openId,
                                         @RequestParam(value = "page",defaultValue = "0")Integer page,
                                         @RequestParam(value = "size",defaultValue = "2")Integer size){
        if(StringUtils.isEmpty(openId)){
            log.error("【查询订单列表】参数为空 ,openId={}",openId);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest request=new PageRequest(page,size);

        return ResultVO.success(orderMasterService.findAll(openId,request).getContent());
    }

    //订单详情

    /**
     * 订单详情
     * @param openId
     * @param orderId
     * @return
     */
    @GetMapping("detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openId,@RequestParam("orderId") String orderId){
        if(StringUtils.isEmpty(orderId)){
            log.error("【查询订单详情】参数为空 ,orderId={}",orderId);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        return ResultVO.success(buyerOrderService.findOneOrder(openId,orderId));
    }


    //取消订单
    @PostMapping("cancel")
    public ResultVO<OrderDTO> cancel(@RequestParam("openid") String openId,@RequestParam("orderId") String orderId){
        if(StringUtils.isEmpty(orderId)){
            log.error("【取消订单】参数为空 ,orderId={}",orderId);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        return ResultVO.success();
    }
}
